package com.hzhr.iot.device.service;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.CygService;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.constant.Device_ErrorsEnum;
import com.hzhr.iot.device.convertor.custom.CollectDeviceCustomConvertor;
import com.hzhr.iot.device.convertor.custom.ImplRecordCustomConvertor;
import com.hzhr.iot.device.convertor.custom.IotDeviceCustomConvertor;
import com.hzhr.iot.device.dao.CollectDeviceDao;
import com.hzhr.iot.device.dao.ImplRecordDao;
import com.hzhr.iot.device.dao.IotDeviceDao;
import com.hzhr.iot.device.entity.CollectDeviceEntity;
import com.hzhr.iot.device.entity.ImplRecordEntity;
import com.hzhr.iot.device.entity.IotDeviceEntity;
import com.hzhr.iot.device.entity.IotDeviceTypeEntity;
import com.hzhr.iot.device.entity.ext.ImplRecordEntityExt;
import com.hzhr.iot.device.entity.ext.IotDeviceEntityExt;
import com.hzhr.iot.device.entity.ext.IotDeviceTypeEntityExt;
import com.hzhr.iot.device.model.collectDevice_collectException.CollectDevice_CollectExceptionGetModel;
import com.hzhr.iot.device.model.collectDevice_implRecord.CollectDevice_ImplRecordGetModel;
import com.hzhr.iot.device.model.collectDevice_implRecord.CollectDevice_ImplRecordModel;
import com.hzhr.iot.device.model.regulatorDevice_monitor.RegulatorDevice_MonitorGetModel;
import com.hzhr.iot.device.queryModel.custom.CollectDeviceCustomQueryModel;
import com.hzhr.iot.device.queryModel.custom.ImplRecordCustomQueryModel;
import com.hzhr.iot.device.queryModel.custom.IotDeviceCustomQueryModel;
import com.hzhr.iot.device.service.base.BaseService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@CygService(entity = ImplRecordEntity.class, model = CollectDevice_ImplRecordModel.class)
public class CollectDevice_ImplRecordService extends BaseService {

    @Autowired
    ImplRecordDao implRecordDao;

    @Autowired
    CollectDeviceDao collectDeviceDao;

    @Autowired
    ImplRecordCustomConvertor implRecordCustomConvertor;


    @Autowired
    CollectDeviceCustomConvertor collectDeviceCustomConvertor;

    @Autowired
    IotDeviceCustomConvertor iotDeviceCustomConvertor;

    @Autowired
    IotDeviceDao iotDeviceDao;

    @Value("${replacePath}")
    String replacePath;


    //region javadoc注释 : get
    /**
     * 查询采集设备的实施档案 <br/><br/>
     * 1. 查询采集设备，如果采集设备不存在抛出异常201； <br/>
     * 2. 查询采集设备的实施档案； <br/>
     * 3. 返回信息； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/><br/>
     * @param collectDeviceId 采集设备编号 <br/><br/>
     * @throws ServiceException <br/>
     * @see CollectDevice_ImplRecordGetModel result <br/>
     */ 
    //endregion
    //region 说明 : get
    //查询采集设备的实施档案
    //1. 查询采集设备，如果采集设备不存在抛出异常201；
    //2. 查询采集设备的实施档案；
    //3. 返回信息；
    //endregion
    //region 参数 : get
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "collectDeviceId", value = "采集设备编号",required = true, dataType = "int", paramType = "path"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
    })
    //endregion
    //region 请求 : get
    @RequestMapping(value = "/collect-devices/{collectDeviceId}/impl-records", method = RequestMethod.GET)
    //endregion
    public CollectDevice_ImplRecordGetModel get(Map<String, String> header, Integer collectDeviceId, Map<String, Object> query) throws ServiceException {
        /* 查询采集设备，如果采集设备不存在抛出异常201 */
        //查询CollectDeviceEntity对象collectDevice_1，查询规则如下：
        //(1) CollectDeviceEntity.id =  路径参数collectDeviceId；
        //(2) CollectDeviceEntity.isValid = '1'；
        //如果collectDevice_1不存在，抛出异常201；
        CollectDeviceCustomQueryModel queryModel = new CollectDeviceCustomQueryModel();
        queryModel.setEqual_id(collectDeviceId);
        queryModel.setEqual_isValid("1");
        queryModel.setShow(true);
        CollectDeviceEntity collectDevice_1 = collectDeviceDao.selectExt(queryModel);
        if (collectDevice_1 == null){
            throw new ServiceException(Device_ErrorsEnum.COLLECTDEVICE_CODE_SERVICE_PUT_201);
        }

        //2. 查询采集设备的实施档案；
        //查询IOT_DEVICE对象集合iotDevices_1，查询规则如下
        //      (1) IOT_DEVICE.ID = collectDevice_1.IOT_DEVICE_ID
        //      (2) IOT_DEVICE.isValid = '1'；
        // 2.1 创建IMPL_RECORD对象集合implRecords_1；
        //for (iotDevice_1 : iotDevices_1)
        //      查询IMPL_RECORD对象implRecord_1，查询规则如下：
        //          (1) IMPL_RECORD.COLLECT_DEVICE_CODE = collectDevice_1.CODE;
        //          (2) IMPL_RECORD.IOT_DEVICE_CODE = iotDevice_1.CODE;
       //           (3) IMPL_RECORD.isValid = '1'；
        //      将implRecord_1加入集合implRecords_1；
//        IotDeviceCustomQueryModel queryModel1 = iotDeviceCustomConvertor.iotDeviceService_query2queryModel(query);
        IotDeviceCustomQueryModel queryModel1 = new IotDeviceCustomQueryModel();
        queryModel1.setEqual_id(collectDevice_1.getIotDeviceId());
        queryModel1.setEqual_isValid("1");
        queryModel1.setShow(true);
        List<IotDeviceEntityExt> iotDevices_1 = iotDeviceDao.selectExtList(queryModel1);

        //设备采集实施档案
        String _show = HzhrTypeUtil.object2string(query.get("_show"));
        List<ImplRecordEntityExt> implRecord_1 = new ArrayList<>();
        if (_show != null) {
            if (iotDevices_1.size() != 0) {
                for (IotDeviceEntityExt iotDeviceEntityExts : iotDevices_1) {
                    ImplRecordCustomQueryModel queryModel2 = new ImplRecordCustomQueryModel();
                    queryModel2.setExt_simTypeExt(true);
                    queryModel2.setShow28(true);
                    queryModel2.setEqual_collectDeviceCode(collectDevice_1.getCode());
                    queryModel2.setEqual_iotDeviceCode(iotDeviceEntityExts.getCode());
                    queryModel2.setEqual_isValid("1");
                    queryModel2.setShow(true);
                    implRecord_1 = implRecordDao.selectExtList(queryModel2);
                }
            }
        }

        //3. 返回信息；
        //根据条件参数,impls_1生成CollectDevice_ImplRecordGetModel对象result并返回
        //ImplRecordConvertor  此处手写返回；未生成
        String[] replacePathSplit = replacePath.split("--");

        CollectDevice_ImplRecordGetModel result = new CollectDevice_ImplRecordGetModel();
        if (implRecord_1.size() > 0 ) {
            if ("one".equals(_show)) {
                ImplRecordEntityExt implRecordEntityExt = implRecord_1.get(0);
                CollectDevice_ImplRecordModel collectDevice_implRecordModel = new CollectDevice_ImplRecordModel();
                collectDevice_implRecordModel.setIotDeviceCode(implRecordEntityExt.getIotDeviceCode());
                collectDevice_implRecordModel.setGasUserName(implRecordEntityExt.getGasUserName());
                collectDevice_implRecordModel.setGasUserAddress(implRecordEntityExt.getGasUserAddress());
                collectDevice_implRecordModel.setImplementer(implRecordEntityExt.getImplementer());
                collectDevice_implRecordModel.setImplementTime(HzhrTypeUtil.date2long(implRecordEntityExt.getImplementTime()));
                collectDevice_implRecordModel.setSimNo(implRecordEntityExt.getSimNo());
                collectDevice_implRecordModel.setSimTypeDes(implRecordEntityExt.getSimTypeExt().getDes());
                String beforeImg = implRecordEntityExt.getBeforeImg();
                String afterImg = implRecordEntityExt.getAfterImg();
                if (replacePathSplit.length == 2) {
                    beforeImg = beforeImg.replace(replacePathSplit[0], replacePathSplit[1]);
                    afterImg = afterImg.replace(replacePathSplit[0], replacePathSplit[1]);
                }
                collectDevice_implRecordModel.setBeforeImg(beforeImg);
                collectDevice_implRecordModel.setAfterImg(afterImg);
                result.setModel(collectDevice_implRecordModel);
            }
        }
        if (implRecord_1.size() > 0) {
            if ("list".equals(_show)) {
                List<CollectDevice_ImplRecordModel> collectDevice_implRecordModels = new ArrayList<>();
                for (ImplRecordEntityExt implRecordEntityExts : implRecord_1) {
                    CollectDevice_ImplRecordModel collectDevice_implRecordModel = new CollectDevice_ImplRecordModel();
                    collectDevice_implRecordModel.setIotDeviceCode(implRecordEntityExts.getIotDeviceCode());
                    collectDevice_implRecordModel.setGasUserName(implRecordEntityExts.getGasUserName());
                    collectDevice_implRecordModel.setGasUserAddress(implRecordEntityExts.getGasUserAddress());
                    collectDevice_implRecordModel.setImplementer(implRecordEntityExts.getImplementer());
                    collectDevice_implRecordModel.setImplementTime(HzhrTypeUtil.date2long(implRecordEntityExts.getImplementTime()));
                    collectDevice_implRecordModel.setSimNo(implRecordEntityExts.getSimNo());
                    collectDevice_implRecordModel.setSimTypeDes(implRecordEntityExts.getSimTypeExt().getTypeDes());// bug
                    //SIM卡类型描述
                    collectDevice_implRecordModel.setSimTypeDes(implRecordEntityExts.getSimTypeExt().getDes());
                    String beforeImg = implRecordEntityExts.getBeforeImg();
                    String afterImg = implRecordEntityExts.getAfterImg();
                    if (replacePathSplit.length == 2) {
                        beforeImg = beforeImg.replace(replacePathSplit[0], replacePathSplit[1]);
                        afterImg = afterImg.replace(replacePathSplit[0], replacePathSplit[1]);
                    }
                    collectDevice_implRecordModel.setBeforeImg(beforeImg);
                    collectDevice_implRecordModel.setAfterImg(afterImg);
                    collectDevice_implRecordModels.add(collectDevice_implRecordModel);
                    result.setModelList(collectDevice_implRecordModels);
                }
            }
        }

        return result;
    }
}

